<?php
get_header();
global $wp_query,$wpdb,$table_prefix;
$current_url = explode('?', cpx_current_url());
$url = $current_url[0];
$category = $wp_query->get_queried_object();
$cat_name = $category->name;
$cat_id = $category->term_taxonomy_id;
$cat_des = $category->description;
?>
<div class="row-fluid" id="category-wrap">
        <h1><?php echo $cat_name;?> Coupon & Promo Codes</h1>
        <div class="row-fluid wrap-des truncated">
            <p class="category-description"><?php echo $cat_des;?> <span class="less_desc">&lsaquo;&lsaquo;Less</span></p>
            <span class="more_desc">&rsaquo;&rsaquo;More</span>
        </div>
</div>
<?php
    $display = get_option('pagination_category');
    if($display == ''){
        $display = 15;
    }
    if(isset($_GET['page']) && (int)$_GET['page']){
        $page = $_GET['page'];
    }
            $my_query ="
                SELECT SQL_CALC_FOUND_ROWS ".$table_prefix."posts.ID as cp_id FROM ".$table_prefix."posts  
                INNER JOIN ".$table_prefix."postmeta ON (".$table_prefix."posts.ID = ".$table_prefix."postmeta.post_id)
                INNER JOIN ".$table_prefix."term_relationships ON (".$table_prefix."postmeta.meta_value= ".$table_prefix."term_relationships.object_id) 
                WHERE 1=1 AND ( ".$table_prefix."term_relationships.term_taxonomy_id IN ($cat_id) ) 
                AND ".$table_prefix."posts.ID = ".$table_prefix."postmeta.post_id
                AND ".$table_prefix."posts.post_type = 'coupon' AND (".$table_prefix."posts.post_status = 'publish') 
                AND ".$table_prefix."postmeta.meta_key = 'store_coupon_metadata'
                GROUP BY ".$table_prefix."postmeta.meta_value ORDER BY ".$table_prefix."posts.post_date DESC
                ";
            $data = mysql_query($my_query);
            $record = mysql_num_rows($data);
            if($record > $display){
                $pages = ceil($record/$display);
            }else{
                $pages = 1;
            }
    $start = (isset($_GET['page']) && (int)$_GET['page'] >= 0) ? (($page - 1)*$display) : 0 ;
    $my_sql ="
        SELECT SQL_CALC_FOUND_ROWS ".$table_prefix."posts.ID as cp_id FROM ".$table_prefix."posts  
        INNER JOIN ".$table_prefix."postmeta ON (".$table_prefix."posts.ID = ".$table_prefix."postmeta.post_id)
        INNER JOIN ".$table_prefix."term_relationships ON (".$table_prefix."postmeta.meta_value= ".$table_prefix."term_relationships.object_id) 
        WHERE 1=1 AND ( ".$table_prefix."term_relationships.term_taxonomy_id IN ($cat_id) ) 
        AND ".$table_prefix."posts.ID = ".$table_prefix."postmeta.post_id
        AND ".$table_prefix."posts.post_type = 'coupon' AND (".$table_prefix."posts.post_status = 'publish') 
        AND ".$table_prefix."postmeta.meta_key = 'store_coupon_metadata'
        GROUP BY ".$table_prefix."postmeta.meta_value ORDER BY ".$table_prefix."posts.post_date DESC LIMIT $start,$display
    ";
    $result = $wpdb->get_results($my_sql);
    $total = count($result);
?>
<div class="row-fluid">
    <div class="span8 content-sidebar">
            <div class="row-fluid boxf tax-store-category">
                        <div class="row-fluid header-coupon">
                            <div class="span12 code-coupon"><h2>Current Top <?php echo $cat_name;?> Coupons</h2></div>
                        </div>
                        <?php
                            foreach ($result as $v){
                            $cp_id = $v->cp_id;
                            cpx_get_coupon_detail_2($cp_id);
                            }
                        ?>
                        <?php 
                            if($pages > 1 && $total > 0){
                                $current = ($start/$display)+1;
                                pagination_custom($url,$page,$pages,$current,2);
                            }
                        ?>
            <?php 
                if($total == 0){ ?>
                <div class="row-fluid notfound">
                    <span>The coupon you need is not available in this category.</span>
                    <p>But you should see these coupons below or go.</p>
                </div>
                <?php
                    $page = get_option('pagination_404');
                    cpx_get_coupon_detail_3('','',$page);
                }
                 ?>
       </div>
    </div>
    <div class="span4 right-sidebar">
         <?php if(is_active_sidebar('cs-main-sidebar')) dynamic_sidebar( 'cs-main-sidebar' ); ?>
    </div>
</div>
<?php get_footer(); ?>
